import {Router} from "express";
import {UsersModel} from "../models/usersModel";
import lodash from "lodash";
import logger from "../logger";

require('express-async-errors');

export const users_router: Router = Router();

export async function users_findUpdate(appid: string, openid: string, updateData: any) {
    return UsersModel.findOneAndUpdate({appid: appid, openid: openid}, lodash.cloneDeep(updateData), {
        new: true,
        runValidators: true,
        upsert: true
    });
}

export async function users_add_ips(appid: string, openid: string, ip: string) {
    try {
        return UsersModel.updateOne({
            openid: openid,
            appid: appid
        }, {$addToSet: {ips: ip}})
    } catch (err) {
        logger.error({msg: 'user_add_ips', err});
    }
}